package com.monitor.app_monitor.service.quartz.utils;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;


@Component
@Aspect
@Slf4j
public class TaskLogAop {

    @Pointcut("@annotation(com.monitor.app_monitor.entity.TaskLogAnnotation)")
    public void pointCut(){}

    @Around("pointCut()")
    public Object handle(ProceedingJoinPoint point) {
        Object[] args = point.getArgs();
        JobExecutionContext jobExecutionContext = (JobExecutionContext) args[0];
        final JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        String JobName = (String) jobDataMap.get("JobName");
        String JobGroup = (String) jobDataMap.get("JobGroup");
        logUtils.quartStartLog(JobName,JobGroup);
        Object object = null;
        try {
            object = point.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        logUtils.quartStopLog(JobName,JobGroup);
        return object;
    }
}
